﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MINERP.MODEL.Statistics;

namespace MINERP.Statistics.ShopBill
{
    public partial class FrmOneShopBillDetail : Form
    {
        private List<MODEL.Statistics.Shop_Info> ListShop = new List<MINERP.MODEL.Statistics.Shop_Info>();    //商户集合 

        private BLL.Statistics.StatisticsBLL SSBLL = new MINERP.BLL.Statistics.StatisticsBLL();
        public FrmOneShopBillDetail()
        {
            InitializeComponent();
        }

        #region 单件
        //单件
        private static FrmOneShopBillDetail _instance;
        public static FrmOneShopBillDetail Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new FrmOneShopBillDetail();
                }
                else if (_instance.IsDisposed)
                {
                    _instance = new FrmOneShopBillDetail();
                }
                return _instance;
            }
        }
        #endregion
        #region Load
        private void FrmOneShopBillDetail_Load(object sender, EventArgs e)
        {
            DateTime now = DateTime.Now;
            string nowStr = now.ToString("yyyy-MM-dd HH:mm");
            dtStartTime.Text = nowStr;
            dtEndTime.Text = nowStr;

            //加载商户
            GetShopList();
            BindShop();
        }
        #endregion

        #region 获取商户列表
        /// <summary>
        /// 获取商户列表
        /// </summary>
        private void GetShopList()
        {
            ListShop.Clear();
            ListShop = SSBLL.GetShopList();
        }
        #endregion

        #region 设置商户下拉列表
        /// <summary>
        /// 设置商户下拉列表
        /// </summary>
        private void BindShop()
        {
            if (cbShopID.Properties.Items != null)
            {
                cbShopID.Properties.Items.Clear();
            }
            foreach (MODEL.Statistics.Shop_Info s in ListShop)
            {
                cbShopID.Properties.Items.Add(s.ShopName);
            }
        }
        #endregion

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (cbShopID.Text == "--请选择商户--")
                {
                    return;
                }
                DateTime startTime = Convert.ToDateTime(dtStartTime.Text);
                DateTime endTime = Convert.ToDateTime(dtEndTime.Text);
                if (startTime >= endTime)
                    return;
                int shopId = 0;
                string shopName = cbShopID.Text;
                foreach (MINERP.MODEL.Statistics.Shop_Info s in ListShop)
                {
                    if (s.ShopName == shopName)
                    {
                        shopId = s.ShopID;
                        break;
                    }
                }
                if (shopId == 0)
                    return;

                //设置账单明细
                List<Bill_Info> ListBill = SSBLL.GetBillInfoNoPay(shopId, startTime, endTime);
                //获取商户账单明细(用List表示)
                List<MODEL.FoundationModel.SHOP_Bill_Info> ListShopBill = SSBLL.GetShop_Bill_InfoFromBill_Info(ListBill);
                //将账单从List转化成DataSet,并将对应的XML文件写入内存
                DataSet billSet = SSBLL.GetBillInfoFromListToDataSet(ListShopBill,0);       //0表示未结单  1表示已结单
                report.Preview = this.previewControl1;
                report.Load(Application.StartupPath + "\\PrintShopBill2.frx");
                report.RegisterData(billSet);
                previewControl1.Refresh();
                previewControl1.Show();
                report.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void FrmOneShopBillDetail_FormClosing(object sender, FormClosingEventArgs e)
        {
            _instance.Dispose();
            _instance = null;
        }
    }
}
